import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKBWriter;
import org.opengis.feature.simple.SimpleFeature;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class GeoData {

    public ArrayList<byte[]> getGeo(String shpPath) {
        FileDataStore fileDataStore = null;
        SimpleFeatureSource simpleFeatureSource;
        SimpleFeatureIterator simpleFeatureIterator = null;
        ArrayList<byte[]> arrayList = new ArrayList<byte[]>();
        try {
            fileDataStore = FileDataStoreFinder.getDataStore(new File(shpPath));
            simpleFeatureSource = fileDataStore.getFeatureSource();
            SimpleFeatureCollection simpleFeatureCollection = simpleFeatureSource.getFeatures();
            simpleFeatureIterator = simpleFeatureCollection.features();
            while (simpleFeatureIterator.hasNext()) {
                SimpleFeature simpleFeature = simpleFeatureIterator.next();
                Geometry geometry = (Geometry) simpleFeature.getDefaultGeometry();
                arrayList.add(new WKBWriter().write(geometry));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileDataStore != null) {
                fileDataStore.dispose();
            }

        }
//        if (simpleFeatureIterator != null){
//            simpleFeatureIterator.close();
//        }
        return arrayList;
    }
}
